package com.tronsis.distribution.activity.me.address;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.AddressBean;
import com.tronsis.distribution.biz.factory.UserBizFactory;
import com.tronsis.distribution.util.CommonUtil;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.MultiItemTypeAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.DisplayUtil;
import com.tronsis.library.widget.ConfirmDialog;
import com.tronsis.library.widget.MyDecoration;
import com.tronsis.library.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AddressManageActivity extends ToolBarBaseActivity {

    @BindView(R.id.rv_address)
    RecyclerView mRvAddress;

    private CommonAdapter<AddressBean> mAddressAdapter;
    private List<AddressBean> mAddressList = new ArrayList<>();

    private String mType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_manage);
        ButterKnife.bind(this);

        if (TextUtils.isEmpty(getIntent().getType())) {
            setTitle(true, getString(R.string.address_manage), getString(R.string.add_new_address));
        } else {
            mType = getIntent().getType();
            setTitle(true, getString(R.string.address_select), getString(R.string.add_new_address));
        }

        initRv();
        getAddress();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            getAddress();
        }
    }

    private void initRv() {
        mAddressAdapter = new CommonAdapter<AddressBean>(mContext, mAddressList, R.layout.item_address) {
            @Override
            protected void convert(ViewHolder holder, AddressBean addressBean, int position) {
                final String name = addressBean.getName();
                final String phone = addressBean.getPhone();
                final String region = CommonUtil.getRegionString(addressBean.getRegion());
                holder.setText(R.id.tv_name, name);
                holder.setText(R.id.tv_phone, phone);
                holder.setText(R.id.tv_region, region + addressBean.getDetail());
            }
        };

        mAddressAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (TextUtils.isEmpty(mType)) {
                    Intent intent = new Intent(mContext, AddressEditActivity.class);
                    intent.setType("edit");
                    intent.putExtra("editAddressBean", mAddressList.get(position));
                    startActivityForResult(intent, 100);
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("AddressBean", mAddressList.get(position));
                    setResult(Activity.RESULT_OK, intent);
                    finish();
                }

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, final int position) {
                new ConfirmDialog(mContext, new ConfirmDialog.OnBottomButtonClickListener() {
                    @Override
                    public void onLeftButtonClick(View view) {

                    }

                    @Override
                    public void onRightButtonClick(View view) {
                        deleteAddress(mAddressList.get(position).getId());
                    }
                }).showDialog("确认删除？", "取消", "确定");


                return true;
            }
        });

        int height = DisplayUtil.getInstance(mContext).dp2px(14);
        int color = Color.parseColor("#F4F4F4");
        RecycleViewDivider decoration = new RecycleViewDivider(mContext, MyDecoration.VERTICAL_LIST, height, color);
        mRvAddress.addItemDecoration(decoration);
        mRvAddress.setLayoutManager(new

                LinearLayoutManager(mContext));
        mRvAddress.setAdapter(mAddressAdapter);
    }

    @Override
    protected void onToolbarClicked(int paramId) {
        super.onToolbarClicked(paramId);
        if (paramId == RIGHT_TEXT) {
            Intent intent = new Intent(mContext, AddressEditActivity.class);
            intent.setType("add");
            startActivityForResult(intent, 101);
        }
    }

    private void getAddress() {
        try {
            UserBizFactory.createBiz().getAddress(this, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    List<AddressBean> addressList = (List<AddressBean>) data;
                    mAddressList.clear();
                    mAddressList.addAll(addressList);
                    mAddressAdapter.notifyDataSetChanged();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }

    private void deleteAddress(final String addrId) {
        try {
            UserBizFactory.createBiz().deleteAddress(this, addrId, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    getAddress();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();

                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }
}
